package cn.zy.framework.util;




/**
 * @Author: Administrator
 * @Description:
 * @Date: Created in 17:08 2021/9/9
 * @Modified By:
 * @version: 1.0
 */
public final class CastUtil {

	/**
	 * 转为String
	 */
	public static String castString(Object obj) {
		return CastUtil.castString(obj, "");
	}

	public static String castString(Object obj, String defaultValue) {

		return obj != null ? String.valueOf(obj) : defaultValue;
	}


	/**
	 * 转为Double
	 */
	public static double castDouble(Object obj) {
		return CastUtil.castDouble(obj, 0);
	}

	public static double castDouble(Object obj, double defaultValue) {

		double value = defaultValue;
		if (obj != null) {
			String strValue = CastUtil.castString(obj);
			if (StringUtil.isNotEmpty(strValue)) {
				try {
					value = Double.parseDouble(strValue);
				} catch (NumberFormatException e) {
					value = defaultValue;
				}
			}
		}

		return value;
	}

	/**
	 * 转为long
	 */
	public static long castLong(Object obj) {
		return CastUtil.castLong(obj, 0);
	}

	public static long castLong(Object obj, long defaultValue) {

		long value = defaultValue;
		if (obj != null) {
			String strValue = CastUtil.castString(obj);
			if (StringUtil.isNotEmpty(strValue)) {
				try {
					value = Long.parseLong(strValue);
				} catch (NumberFormatException e) {
					value = defaultValue;
				}
			}
		}

		return value;
	}


	/**
	 * 转为int
	 */
	public static int castInt(Object obj) {
		return CastUtil.castInt(obj, 0);
	}

	public static int castInt(Object obj, int defaultValue) {

		int value = defaultValue;
		if (obj != null) {
			String strValue = CastUtil.castString(obj);
			if (StringUtil.isNotEmpty(strValue)) {
				try {
					value = Integer.parseInt(strValue);
				} catch (NumberFormatException e) {
					value = defaultValue;
				}
			}
		}

		return value;
	}


	/**
	 * 转为boolean
	 */
	public static boolean castBoolean(Object obj) {
		return CastUtil.castBoolean(obj, false);
	}

	public static boolean castBoolean(Object obj, boolean defaultValue) {
		boolean value = defaultValue;
		if (obj != null) {
			value = Boolean.parseBoolean(castString(obj));
		}
		return value;
	}
}
